package com.ruixi.gen;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import com.ruixi.core.BaseController;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import java.util.function.Consumer;

/**
 * @Created by xxxx
 * @Date 2022/8/10 15:09
 * @Description 代码生成器
 * 具体生成策略参考 https://baomidou.com/pages/981406/#service-%E7%AD%96%E7%95%A5%E9%85%8D%E7%BD%AE
 */
@Slf4j
public class CodeGenerator {

    @Autowired
    private static Environment env;

    public static void main(String[] args) {

//        String url = env.getProperty("server.port");
//
//        log.info("url:"+url);
        //项目根路径
        String projectPath = System.getProperty("user.dir");
        log.info("projectPath:"+projectPath);

        // 数据源配置
        DataSourceConfig.Builder dataSourceConfig = new DataSourceConfig
                .Builder(
                "jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC",
                "root",
                "root")
                .dbQuery(new MySqlQuery())
                .typeConvert(new MySqlTypeConvert())
                .keyWordsHandler(new MySqlKeyWordsHandler());

        FastAutoGenerator.create(dataSourceConfig)
                //全局配置(GlobalConfig)
                .globalConfig(builder -> {
                    builder.author("lzg") //设置作者
                            .commentDate("YYYY-MM-DD HH:mm:ss")//注释日期
//                            .fileOverride()
                            .outputDir(projectPath + "/codegenerator"); //指定输出目录

                })
                //Controller 策略配置-设置父类
                .strategyConfig(builder -> {
                    builder.controllerBuilder().
                            superClass(BaseController.class);
                })
                //包配置(PackageConfig)
                .packageConfig(builder -> {
                    builder.parent("com.ruixi") // 设置父包名
                    .entity("domain");//Entity 包名
                })
                //策略配置(StrategyConfig)
                .strategyConfig(builder -> {
//                    builder.addInclude("tb_user") // 设置需要生成的表名
//                            .addTablePrefix("tb_"); // 设置过滤表前缀
                    builder.addInclude("role") // 设置需要生成的表名
                            .addTablePrefix(""); // 设置过滤表前缀
                    builder.entityBuilder().enableLombok();//开启 lombok 模型
                    builder.entityBuilder().enableTableFieldAnnotation();//开启生成实体时生成字段注解
                    builder.controllerBuilder().enableRestStyle(); //开启生成@RestController 控制器
                    builder.mapperBuilder().enableMapperAnnotation(); //Mapper 策略配置,开启 @Mapper 注解

                })
                //模板配置(TemplateConfig)
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

}
